package com.cnciems.shardingsphere.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Ordering;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
* @program: study
* @package: com.cnciems.shardingsphere.order
* @description: TODO
* @author: Haijiang Hu
* @dep R&D SP Co.,Ltd
* @create: 2020-08-19 21:41
* @version V1.0
**/
@RestController
@AllArgsConstructor
@RequestMapping("order")
public class OrderController {
    private OrderMapper orderMapper;

    @GetMapping("/insert")
    public int insert(){
        Order o ;
        for (int i = 1; i <=20 ; i++) {
            o = new Order();
//            o.setUserId(i);  使用系统给定的id,需要修改数据库中userId 字段的类型为varchar ,分库分表中，根据userId分库也需要修改
            o.setOrderId(new Random().nextInt(100));
            orderMapper.insert(o);
        }
        return 1;
    }

    @GetMapping("/get/{userId}")
    public Order getById(@PathVariable("userId") Integer userId){
        return orderMapper.selectById(userId);
    }

    @GetMapping("/get/{userId}/{orderId}")
    public Order getById(@PathVariable("userId") Integer userId,@PathVariable("orderId") Integer orderId){
        QueryWrapper<Order> q = new QueryWrapper<>();
        q.eq("user_id",userId);
        q.eq("order_id",orderId);
        return orderMapper.selectOne(q);
    }
}
